x_func = @(t) t;  
y_func = @(t) t.^3 + t;  
    
num_points = 10;  
  
num_points_initial = 1000;  
  
t = linspace(0, 1, num_points_initial);  
  
xP = x_func(t);  
yP = y_func(t);  
  
dx_dt = gradient(xP) ./ gradient(t);  
dy_dt = gradient(yP) ./ gradient(t);  
  
dl = sqrt(dx_dt.^2 + dy_dt.^2);  
  
cumulative_arc_length = cumsum(dl);  
  
total_arc_length = cumulative_arc_length(end);  
  
desired_arc_length_interval = total_arc_length / (num_points - 1);  
  
new_t = zeros(num_points, 1);  
new_t(1) = t(1);
  
for i = 2:num_points  
    target_arc_length = (i - 1) * desired_arc_length_interval;  
       
    [~, idx] = min(abs(cumulative_arc_length - target_arc_length));  
        
    if idx < length(cumulative_arc_length)  
        alpha = (target_arc_length - cumulative_arc_length(idx-1)) / (cumulative_arc_length(idx) - cumulative_arc_length(idx-1));  
        new_t(i) = t(idx-1) + alpha * (t(idx) - t(idx-1));  
    else  
        new_t(i) = t(end); 
    end  
end  
   
new_xP = x_func(new_t);  
new_yP = y_func(new_t);  
 
new_dx_dt = gradient(new_xP) ./ gradient(new_t);  
new_dy_dt = gradient(new_yP) ./ gradient(new_t);  
    
new_dx2_dt2 = gradient(new_dx_dt, 2) ./ gradient(new_t, 2);  
new_dy2_dt2 = gradient(new_dy_dt, 2) ./ gradient(new_t, 2);  
  
curvatures = abs(new_dx_dt(2:end-1) .* new_dy2_dt2(2:end-1) - new_dy_dt(2:end-1) .* new_dx2_dt2(2:end-1)) ./ ...  
             (new_dx_dt(2:end-1).^2 + new_dy_dt(2:end-1).^2).^(3/2);  
disp('原始的x：');  
disp(new_t);  
disp('等间距弧长下的x꣺');  
disp(new_xP);  
disp('等间距弧长下的y꣺');  
disp(new_yP);  
disp('等间距采样点的曲率');  
disp(curvatures);

err=10000;
coefficient=[0,0,0,0];

y_ini=-2+6*rand(1,10);
degree = 3;
x = 1:length(y_ini);
p = polyfit(x, y_ini, 3);
y_fit = polyval(p, x);
x_fine = linspace(min(x), max(x), 1000); 
y_fine = polyval(p, x_fine); 
figure;
plot(x, y_ini, 'ko', 'MarkerFaceColor', 'r');
hold on;

plot(x_fine, y_fine, 'b-'); 

plot(x, y_ini, 'r-');

legend('原始数据点', '拟合曲线', '原始曲线');
title('原始');

xlabel('x');
ylabel('y');
err1=10000;
err2=10000;
grid on;

for i=1:1000
y_ini=-2+2*rand(1,10);
degree = 3;
x = 1:length(y_ini);
p = polyfit(x, y_ini, 3);
y_fit = polyval(p, x);
x_fine = linspace(min(x), max(x), 1000); 
y_fine = polyval(p, x_fine); 
kap_return=KAPPA(p(1),p(2),p(3),new_xP);
kap_return = kap_return(2:end);
kap_return = kap_return(1:end-1);  
sum3=0;
for j=1:8
    sum3=(abs(curvatures(j)-kap_return(j)))^2+sum3;
end
sum3=(sum3)/8;

sum1=0;
variance1=0;
for j=1:8
    sum1=sum1+kap_return(j);
end
for k=1:8
variance1=variance1+(abs(kap_return(k)-sum1)^2/8);
end
if sum3<err1
    if variance1<err2
        err1=sum3;
        err2=variance1;
    end
     coefficient=p;
end
end
polyFunc1 = @(x) coefficient(1)*x.^3 +coefficient(2)*x.^2 + coefficient(3)*x + coefficient(4); 

xValues = linspace(-5, 5, 1000); 
  
yValuesPoly = polyFunc1(xValues); 
yValuesOriginal = xValues.^3 + xValues;  
  
figure; 
plot(xValues, yValuesPoly, 'b-'); 
hold on; 
  
plot(xValues, yValuesOriginal, 'r--'); 
  
legend('优化后的拟合曲线', '原始曲线y=x^3+x');  
  
title('优化后曲线');  
xlabel('x');  
ylabel('y');  
grid on; 